package com.hmall.cart.listener;

import com.hmall.cart.service.ICartService;
import lombok.RequiredArgsConstructor;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.util.Set;

@Component
@RequiredArgsConstructor
public class ClearCartListener {

    private final ICartService iCartService;

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue("cart.clear.queue"),
            exchange = @Exchange(value = "trade.topic",type = "topic"),
            key = "order.create"
    ))
    public void listenclearcart(Set<Long> itemsId, Message message) {
        Long userId = message.getMessageProperties().getHeader("user_INFO");
        iCartService.removeByItemIdsAndUserId(itemsId,userId);
    }
}
